package com.consume.bhy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.consume.bhy.entity.ConsumeEntity;
import com.consume.bhy.entity.PageEntity;
import com.consume.bhy.entity.ShowEntity;
import com.consume.bhy.jdbc.JDBC_Util;
import com.consume.bhy.service.ConsumeService;



@WebServlet(urlPatterns="/consume/consumeServlet")
@SuppressWarnings("unused")
public class ConsumeServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		
		Class<?> class1 = this.getClass();
		
		String methodName = request.getParameter("method");
		
		try {
			Method method = class1.getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
			method.setAccessible(true);
			try {
				method.invoke(this, request,response);
				
			} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
				e.printStackTrace();
			}
			
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		}
	}
	
	
	private void addConsume(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException, ServletException, ParseException {
		request.setCharacterEncoding("utf-8");
		
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		String count = request.getParameter("count");
		String typeName = request.getParameter("typeName");
		
		
		String date = request.getParameter("date");

		String remark = request.getParameter("remark");
		
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		
		Date date2 = sd.parse(date);
		
		ConsumeService consumeService = new ConsumeService();
		
		ConsumeEntity consume = new ConsumeEntity();
		
		consume.setName(name);
		consume.setPrice(Double.valueOf(price));
		consume.setCount(Integer.parseInt(count));
		consume.setRemark(remark);
		consume.setDate(date2);
		
	
		int typeId = 0;
		Connection conn = JDBC_Util.getConnection();
		String sql3 = "select consume_type_id from consume_type where consume_type_name=?";
		PreparedStatement preparedStatement3 = conn.prepareStatement(sql3);
		preparedStatement3.setString(1,typeName );
		ResultSet result3 = preparedStatement3.executeQuery();
		if(result3.next()) {
			typeId = result3.getInt("consume_type_id");
			consume.setTypeId(typeId);
		}
		JDBC_Util.clossJDBC(conn,preparedStatement3, result3);
	
			boolean result = consumeService.addConsume(consume);
			if (result) {
				request.getSession().setAttribute("msg", "录入成功");
//				request.getRequestDispatcher("/consume/consumeServlet?method=searchConsume").forward(request, response);
				response.sendRedirect(request.getContextPath()+"/consume/consumeServlet?method=searchConsume");
			} else {
				
				request.setAttribute("consume", consume);
				request.setAttribute("msg", "录入失败");
				request.getRequestDispatcher("/consume-add.jsp").forward(request, response);
			}
	}
	
	private void showAddConsume(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException, ServletException, ParseException {
		
		request.setCharacterEncoding("utf-8");

		ConsumeService cla = new ConsumeService();
		PageEntity<List<ShowEntity>> pageModel = cla.showAddConsume();
		
		request.setAttribute("classList", pageModel.getClassList());
		request.getRequestDispatcher("/consume-add.jsp").forward(request, response);
			
	}
	
	private void searchConsume(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException, ServletException {
		
		request.setCharacterEncoding("utf-8");
		
		String pageNo = request.getParameter("pageNo");
		
		String searchChoose = request.getParameter("search-choose");
		String searchContent = request.getParameter("search-content");
		
		ConsumeService cla = new ConsumeService();
		PageEntity<List<ShowEntity>> pageModel = cla.searchConsume(searchContent,searchChoose,pageNo);
		
		request.setAttribute("pageModel", pageModel);
		request.setAttribute("searchChoose",searchChoose);
		request.setAttribute("searchContent",searchContent);
	
		request.getRequestDispatcher("/consume-info.jsp").forward(request, response);

	}
	
	private void updateConsume(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException, ServletException {
		
		
		request.setCharacterEncoding("utf-8");
		
		
		String id = request.getParameter("consumeId");
		
		ConsumeService cla = new ConsumeService();
		ConsumeEntity result = cla.updateConsume(id);
		
		String typeName = null;
		
		Connection conn = JDBC_Util.getConnection();
		String sql = "select consume_type_name from consume_type where consume_type_id=?";
		PreparedStatement preparedStatement = conn.prepareStatement(sql);
		preparedStatement.setInt(1, result.getTypeId());
		ResultSet result2 = preparedStatement.executeQuery();
	
		if(result2.next()) {
			typeName = result2.getString("consume_type_name");
		}

		List<String> typeNamelist = new ArrayList<String>();
		
		typeNamelist = cla.typeNameList();
		
		request.setAttribute("typeName", typeName);
		request.setAttribute("classList", typeNamelist);
		request.setAttribute("consume", result);
		request.getRequestDispatcher("/consume-update.jsp").forward(request, response);
	}
	
	
	private void updateConsumeInfo(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException, ParseException {
		
		request.setCharacterEncoding("utf-8");
		
		response.setContentType("text/plain; charset=UTF-8");
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		String count = request.getParameter("count");
		String date = request.getParameter("date");
		String typeName = request.getParameter("typeName");
		String remark = request.getParameter("remark");

		ConsumeEntity consume = new ConsumeEntity();
		consume.setId(Integer.parseInt(id));
		consume.setName(name);
		consume.setPrice(Double.valueOf(price));
		consume.setCount(Integer.parseInt(count));
		
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		
		Date date2 = sd.parse(date);
		
		consume.setDate(date2);
		
		int typeId=0;
		
		Connection conn = JDBC_Util.getConnection();
		String sql = "select consume_type_id from consume_type where consume_type_name=?";
		PreparedStatement preparedStatement = conn.prepareStatement(sql);
		preparedStatement.setString(1, typeName);
		ResultSet result2 = preparedStatement.executeQuery();
	
		if(result2.next()) {
			typeId = result2.getInt("consume_type_id");
		}
		
		consume.setTypeId(typeId);

		consume.setRemark(remark);
		
		
		ConsumeService consumeService = new ConsumeService();
		
		boolean result = consumeService.updateConsumeInfo(consume);
		
		if(result) {
			request.removeAttribute("consume");
			request.setAttribute("consume", consume);
		}
		
		PrintWriter pw = response.getWriter();
		pw.print(result);
		pw.close();
	}
	
	private void deleteConsume(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException, ServletException {
		
		request.setCharacterEncoding("utf-8");
		
	
		int id = Integer.parseInt(request.getParameter("consumeId"));
		

		ConsumeEntity consume = new ConsumeEntity();
	
		
		ConsumeService consumeService = new ConsumeService();
		
		boolean result = consumeService.deleteConsume(id);
		
		if(result) {
			request.setAttribute("msg", "删除成功");
			request.getRequestDispatcher("/consume/consumeServlet?method=searchConsume").forward(request, response);
		}else {
			request.setAttribute("msg", "删除失败");
			request.getRequestDispatcher("/consume/consumeServlet?method=searchConsume").forward(request, response);
		}
	}
	
	
}
